﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using LuckyLoot.ServiceFacade.Implementation;
using LuckyLoot.ServiceFacade.Contracts;
using System.Web.Script.Serialization;
using System.ServiceModel.Web;
using System.Text;
using LuckyLoot.Common.Validation;
using LuckyLoot.ClientDataProviderServices.DataTransferObjects;
using LuckyLoot.Common;

namespace LuckyLoot.ClientDataProviderServices
{
    public class MassDataProviderService : IMassDataProviderService
    {
        public Stream GetApointmentPhotos(string apointmentID)
        {
            if (string.IsNullOrWhiteSpace(apointmentID))
                return null;

            int id = Int32.Parse(apointmentID);
            if (id < 1)
                return null;

            IApointmentService apointmentService = new ApointmentService();
            var photos = apointmentService.GetApointmentPictures(id);

            if (photos.Count == 0)
                return null;
            
            var pictures = new List<PictureDTO>();
            foreach (var pic in photos)
            {
                pictures.Add(new PictureDTO() { Id = pic.Id, Content = BinaryImageHandler.ByteArrayToString(pic.Content) });
            }

            var jsonSerializer = new JavaScriptSerializer();
            var artistsResponse = jsonSerializer.Serialize(pictures);

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return new MemoryStream(Encoding.UTF8.GetBytes(artistsResponse));
        }

        
    }
}